package com.lyq.permission.controller;

import com.lyq.permission.domain.Menu;
import com.lyq.permission.service.IMenuService;
import com.lyq.basic.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    /**
     * 查询某用户他拥有的菜单树
     * 解决方案:
     *  方案一: 在后端获取当前登陆用户,拿到登陆用户的ID查询菜单树
     *  方案二: 在登陆成功以后,将当前登陆用户传递给前端,前端将登陆用户信息存到浏览器,在进行查询时,前端拿到登录用户的id传给后端(采纳这种)
     * @return
     */
    @GetMapping("/tree")
    public R tree(Long userId){
        try {
            List<Menu> menus = menuService.tree(userId);
            return R.me().setData(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询菜单树失败!"+e.getMessage());
        }
    }

}
